package avicit.bdp.dcs.threshold.other;

import avicit.bdp.dcs.threshold.utils.TestUtils;
import avicit.platform6.core.rest.msg.ResponseMsg;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

import static org.junit.Assert.assertEquals;

/**
 * 任务启动、停止、删除等基本操作
 */
public class ExecutorTest {
    private RestTemplate restTemplate = new RestTemplate();

    private String jobId = null;

    private static String srcTble = "src_executor_test1";

    private static String dstTble = "dst_executor_test1";

    /**
     * 所有用例执行前准备工作
     */
    @BeforeClass
    public static void init() {
        TestUtils.prepareData(srcTble);
    }

    /**
     * 单个测试用例运行前准备工作
     */
    @Before
    public void setUp() {
        // TODO:
    }

    /**
     * 单个测试用例运行后销毁工作
     */
    @After
    public void tearDown() {
        // TODO:
    }

    /**
     * 所有用例执行完成后准备工作
     */
    @AfterClass
    public static void destory() {
        TestUtils.deleteFromMysql(srcTble);
        TestUtils.deleteFromMysql(dstTble);
    }

    /**
     * 新建Job
     */
    private void createJob() {
        // step1:保存数据源
        ResponseEntity<ResponseMsg> result = restTemplate.postForEntity(TestUtils.HOSTPORT
                        + "/api/bdp/dcs/job/JobRest/saveDataSource", TestUtils.buildSaveDataSourceParas(2,
                "Execute门槛用例", TestUtils.MYSQL_SRC_DATASOURCE_ID, TestUtils.MYSQL_DST_DATASOURCE_ID, 1),
                ResponseMsg.class);
        TestUtils.handleFailedRequest(result);
        assertEquals("200", result.getBody().getRetCode());
        // 获取创建的JobId
        ResponseMsg responseMsg = result.getBody();
        Map<String, String> jobMap = (Map<String, String>) responseMsg.getResponseBody();
        jobId = jobMap.get("jobId");

        // step2:保存表映射关系
        result = restTemplate.postForEntity(TestUtils.HOSTPORT + "/api/bdp/dcs/job/JobRest/saveTableMapping",
                TestUtils.buildSaveTblMapping(2, jobId, srcTble, dstTble), ResponseMsg.class);
        TestUtils.handleFailedRequest(result);
        assertEquals("200", result.getBody().getRetCode());

        // step3:任务配置检查
        result = restTemplate.postForEntity(TestUtils.HOSTPORT + "/api/bdp/dcs/job/JobRest/checkTaskConfig",
                TestUtils.buildCheckTaskConfig(jobId), ResponseMsg.class);
        TestUtils.handleFailedRequest(result);
        assertEquals("200", result.getBody().getRetCode());

        // step4:预检查
        result = restTemplate.postForEntity(TestUtils.HOSTPORT + "/api/bdp/dcs/precheck/PrecheckRest" +
                        "/executePrecheck",
                TestUtils.buildPreChecks(jobId), ResponseMsg.class);
        TestUtils.handleFailedRequest(result);
        assertEquals("200", result.getBody().getRetCode());

        // step5:更新Job状态
        result = restTemplate.postForEntity(TestUtils.HOSTPORT + "/api/bdp/dcs/job/JobRest/updateJobStatus",
                TestUtils.buildUpdateJobStatus(jobId), ResponseMsg.class);
        TestUtils.handleFailedRequest(result);
        assertEquals("200", result.getBody().getRetCode());
    }

    /***********************************************Begin 测试用例 ***********************************************/
    /**
     * step1:手动执行；step2:手动执行;(预期结果：success,faile)
     *
     * @throws Exception
     */
    @Test(timeout = 5 * 60 * 1000)
    public void testExecute1() throws Exception {
        try {
            // step1:创建任务
            createJob();

            // step2:启动任务，预期success
            ResponseEntity<ResponseMsg> result = restTemplate.postForEntity(TestUtils.HOSTPORT + "/api/bdp/dcs" +
                    "/job/JobRest/startJobByIds?", TestUtils.buildStartJob(jobId), ResponseMsg.class);
            TestUtils.handleFailedRequest(result);
            assertEquals("200", result.getBody().getRetCode());

            // step3:再次启动采集任务，预期fail
            // TODO:实际还是可以启动；20200914
            Thread.sleep(10 * 1000);
            result = restTemplate.postForEntity(TestUtils.HOSTPORT + "/api/bdp/dcs" +
                    "/job/JobRest/startJobByIds?", TestUtils.buildStartJob(jobId), ResponseMsg.class);
            TestUtils.handleFailedRequest(result);
            assertEquals("500", result.getBody().getRetCode());
        } finally {
            // step4: 删除job
            ResponseEntity<ResponseMsg> result = restTemplate.exchange(TestUtils.HOSTPORT + "/api/bdp/dcs/job/JobRest" +
                            "/deleteJobByIds",
                    HttpMethod.DELETE,
                    TestUtils.buildDeleteJob(jobId),
                    ResponseMsg.class);
            TestUtils.handleFailedRequest(result);
            assertEquals("200", result.getBody().getRetCode());
        }
    }

    /**
     * step1:手动执行；step2:停止;(预期结果：success,faile)
     *
     * @throws Exception
     */
    @Test(timeout = 5 * 60 * 1000)
    public void testExecute2() throws Exception {
        try {
            // step1:创建任务
            createJob();

            // step2:启动任务，预期success
            ResponseEntity<ResponseMsg> result = restTemplate.postForEntity(TestUtils.HOSTPORT + "/api/bdp/dcs" +
                    "/job/JobRest/startJobByIds", TestUtils.buildStartJob(jobId), ResponseMsg.class);
            TestUtils.handleFailedRequest(result);
            assertEquals("200", result.getBody().getRetCode());

            // step3:停止采集任务，预期success
            Thread.sleep(10 * 1000);
            result = restTemplate.postForEntity(TestUtils.HOSTPORT + "/api/bdp/dcs" +
                    "/job/JobRest/finishJobByIds", TestUtils.buildStartJob(jobId), ResponseMsg.class);
            TestUtils.handleFailedRequest(result);
            assertEquals("200", result.getBody().getRetCode());
        } finally {
            // step4: 删除job
            ResponseEntity<ResponseMsg> result = restTemplate.exchange(TestUtils.HOSTPORT + "/api/bdp/dcs/job/JobRest" +
                            "/deleteJobByIds",
                    HttpMethod.DELETE,
                    TestUtils.buildDeleteJob(jobId),
                    ResponseMsg.class);
            TestUtils.handleFailedRequest(result);
            assertEquals("200", result.getBody().getRetCode());
        }
    }

    /**
     * step1:手动执行；step2:删除;(预期结果：success,faile)
     *
     * @throws Exception
     */
    @Test(timeout = 5 * 60 * 1000)
    public void testExecute3() throws Exception {
        // step1:创建任务
        createJob();

        // step2:启动任务，预期success
        ResponseEntity<ResponseMsg> result = restTemplate.postForEntity(TestUtils.HOSTPORT + "/api/bdp/dcs" +
                "/job/JobRest/startJobByIds", TestUtils.buildStartJob(jobId), ResponseMsg.class);
        TestUtils.handleFailedRequest(result);
        assertEquals("200", result.getBody().getRetCode());

        // step3:删除采集任务，预期success
        Thread.sleep(10 * 1000);
        result = restTemplate.exchange(TestUtils.HOSTPORT + "/api/bdp/dcs/job/JobRest" +
                        "/deleteJobByIds",
                HttpMethod.DELETE,
                TestUtils.buildDeleteJob(jobId),
                ResponseMsg.class);
        TestUtils.handleFailedRequest(result);
        assertEquals("200", result.getBody().getRetCode());
    }

    /**
     * step1:创建任务；step2:停止;(预期结果：success,faile)
     *
     * @throws Exception
     */
    @Test(timeout = 5 * 60 * 1000)
    public void testExecute4() throws Exception {
        // step1:创建任务
        createJob();

        // step2:停止采集任务，预期fail
        Thread.sleep(10 * 1000);
        ResponseEntity<ResponseMsg> result = restTemplate.postForEntity(TestUtils.HOSTPORT + "/api/bdp/dcs" +
                        "/job/JobRest/finishJobByIds",
                TestUtils.buildStartJob(jobId), ResponseMsg.class);
        TestUtils.handleFailedRequest(result);
        assertEquals("200".equals(result.getBody().getRetCode()), false);

        // step3:删除采集任务，预期success
        Thread.sleep(10 * 1000);
        result = restTemplate.exchange(TestUtils.HOSTPORT + "/api/bdp/dcs/job/JobRest" +
                        "/deleteJobByIds",
                HttpMethod.DELETE,
                TestUtils.buildDeleteJob(jobId),
                ResponseMsg.class);
        TestUtils.handleFailedRequest(result);
        assertEquals("200", result.getBody().getRetCode());
    }

    /**
     * step1:创建任务；step2:删除;(预期结果：success,success)
     *
     * @throws Exception
     */
    @Test(timeout = 5 * 60 * 1000)
    public void testExecute5() throws Exception {
        // step1:创建任务
        createJob();

        // step2:删除采集任务，预期success
        Thread.sleep(10 * 1000);
        ResponseEntity<ResponseMsg> result = restTemplate.exchange(TestUtils.HOSTPORT + "/api/bdp/dcs/job/JobRest" +
                        "/deleteJobByIds",
                HttpMethod.DELETE,
                TestUtils.buildDeleteJob(jobId),
                ResponseMsg.class);
        TestUtils.handleFailedRequest(result);
        assertEquals("200", result.getBody().getRetCode());
    }

}
